






    $(function () {



        $('#user-login-btn').on('click',function () {
            $('.error').css('display','none');

            let username =$('#login-username').val();
            let password = $('#login-password').val();

            console.log(username);
            user.login(username,password)
        })



        $('#register').on('click',function () {

            $('.error').css('display','none');
            let username = $('#reg_username').val();
            let email = $('#reg_email').val();
            let password = $('#reg_password').val();
            let repassword = $('#reg_repassword').val();
            let phone = $('#reg_phone').val();
            let question = $('#reg_question').val();
            let answer =$('#reg_answer').val();
            let flag = 1;
            if (username == ""){
                $('#username-error').css('display','inline-block').html('用户名不能为空')
                flag=0;
            }

            if (email == ""){
                $('#email-error').css('display','inline-block').html('邮箱不能为空')
                flag=0;
            }else if (!_common_util.validate(email,'email')){
                $('#email-error').css('display','inline-block').html('邮箱格式错误');
                flag=0;

            }

            if (phone==""){
                $('#phone-error').css('display','inline-block').html('电话号码不能为空');
                flag=0;
            }else if (!_common_util.validate(phone,'phone')){
                $('#phone-error').css('display','inline-block').html('电话号码格式错误');
                flag=0;
            }


            if ( password == ""){
                $('#password-error').css('display','inline-block').html('请输入密码')
                flag=0;
            }else if(repassword != password){
                $('#repassword-error').css('display','inline-block').html('两次输入密码不一致')
                flag=0;
            }

            if (flag == 1){
                user.register(username,password,phone,email,question,answer);
            }
        });

        $('#reset-password-first').css('display','none');
        $('#reset-password-second').css('display','none');
        $('#reset-password-third').css('display','none');
        $('#reset-password-success').css('display','none');

        $('#lost-password').on('click',function () {
            $('#customer_login').css('display','none');
            $('.or-text').css('display','none');
            $('#reset-password-first').css('display','block');
        })


        $('#reset-password-first-btn').on('click',function () {
            let resetusername = $('#forget-username').val();
            user.getQuestionByUsername(resetusername);
            if ('fquestion' in localStorage){
                $('#reset-password-first').css('display','none');
                $('#reset-password-second').css('display','block')

                let question =  window.localStorage.getItem('fquestion');
                $('#forget-user-question').val(question);
                $('#forget-username-errorr').css('display','none');
                $('#reset-password-first').css('display','none');
                $('#reset-password-second').css('display','block');

            }else {
                $('#forget-username-errorr').css('display','inline-block').html('该用户名不存在')
                console.log('username not find');
            }

        })

        $('#reset-password-second-btn').on('click',function () {
            let inputanswer =$('#forget-user-answer').val();
            let username = $('#forget-username').val();
            $.ajax({
                type        : 'POST',
                url         : _common_util.getServerURL('admin/check_answer'),
                dataType    :  'json',
                data: {
                    'username'  : username,
                    'question'  : localStorage.getItem('fquestion'),
                    'answer'    : inputanswer
                },
                success :function (res) {
                    if (res.status === 1 ){
                        console.log('回答错误');
                        $("#forger-qusetin-answer-error").css('display','inline-block').html("回答错误");
                    }else if (res.status === 0){
                        console.log('回答正确');
                        localStorage.setItem('forgetToken',res.data);
                        $("#forger-qusetin-answer-error").css('display','none')
                        $('#reset-password-second').css('display','none');
                        $('#reset-password-third').css('display','block');
                    }
                }
            })

        })

        $('#reset-password-third-btn').on('click',function () {
            let username= $('#forget-username').val();
            let password = $('#forget-user-password').val();
            let repassword = $('#forget-user-repassword').val();
            let forgetToken = localStorage.getItem('forgetToken');
            console.log(forgetToken)
            if (password === repassword){
                $.ajax({
                    type        : 'POST',
                    url         :  _common_util.getServerURL("admin/forget_reset"),
                    dataType    :  'json',
                    data: {
                        'username' : username,
                        'newPassword' : password,
                        'forgetToken' : forgetToken
                    },
                    success:function (res) {
                        if (res.status === 0){
                            console.log('修改密码成功');
                            localStorage.clear();
                            $('#reset-password-third').css('display','none');
                            $('#reset-password-success').css('display','block');
                            setTimeout(_common_util.doLogin,5000)
                        }
                        // _common_util.sleep(5000);
                        // _common_util.doLogin();
                    }
            })
            }
        })
    })


